<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: TranslatecontentController.php 465 2012-02-12 05:33:23Z beimuaihui $
 */



class Multilang_TranslatecontentController extends Baogg_Controller_Action {

	function init() {
	    Zend_Loader::loadClass ( 'TranslateContent' );
		$this->translateContent = new TranslateContent ( );
		
		Zend_Loader::loadClass ( 'Language' );
		$this->language = new Language ( );

	}

	/**
	 * The default action
	 */
	public function listAction() {
		global $LANG;
		
		Baogg_Language::loadFile("translatevar");
		Baogg_Language::loadFile("category");
		Baogg_Language::loadFile("language");
		
		Zend_Loader::loadClass("Baogg_View_Extgrid");
		
		$cm=array();
		$cm['translate_var_id']=array("hidden"=>true,"cmType"=>Baogg_View_Extgrid::CMTYPE_PK) ;
		$cm['translate_var_name']=array("sortable"=>true);
		$cm['translate_var_type']=array("sortable"=>true);
        //$cm['category_name']=array("sortable"=>true); #TODO add translate var category
       // $cm['content']=array("sortable"=>true);
		
        $arr_language_id=array();
        $rs_language=$this->language->getList(NULL,array("sort"=>"is_default","dir"=>"desc"));		//array("is_default"=>0)
		foreach((array)$rs_language as $v){
			if($v['language_id']!=BAOGG_LANGUAGE_ID)
			{
				$arr_language_id[$v['language_id']]=$v['language_name'];
			}
		    $cm['content_'.$v['language_id']]=array("sortable"=>false,"header"=>BAOGG_LANGUAGE::get('content').'('.$v['language_name'].')');
		}
		
		
		$grid=new Baogg_View_Extgrid(BAOGG_BASE_URL."multilang/translatecontent/data", $cm);
		$grid->setSelectionModel(true);
		$grid->setRowNumberer(true);
	//$grid->registerButton("delete","grid.deleteSelected('".BAOGG_BASE_URL."multilang/translatecontent/delete/')");
	
		
		$this->view->form_url=BAOGG_BASE_URL."multilang/translatecontent/save";
		$LANG['store_translate_var_type']=Baogg_Language::array2store("arr_translate_var_type");
		
		
		//echo '<pre>';print_r($rs_language);print_r($arr_language_id);print_r();exit;
				
		$LANG['store_language_id']=Baogg_Language::array2store($arr_language_id);
		
		$this->view->LANG=Zend_Json::encode($LANG);
		$this->view->grid = $grid->render();
	}

	/**
	 * The data action
	 */
	function dataAction() {
	    $post=$this->_request->getPost ();
	    if(isset($post['translate_var_id'])){
	    	if(isset($post['translate_var_type_id'])){
	    	    $post['translate_var_type']=$post['translate_var_type_id'];
	    	    unset($post['translate_var_type_id']);
	    	}
    	    //unset($post['content']); #TODO this must remove after translate_content module finish
	    }
	    
	    $where=array("language_id"=>(int)BAOGG_LANGUAGE_ID);$order=array();$limit=array();
	    
	   foreach((array)$post as $k=>$v){
	       if($k=="start" || $k=="limit"){
	           $limit[$k]=$v;
	       }else if($k=="sort" || $k=="dir"){
	           $order[$k]=$v;
	       }else{
	           if($k=="translate_var_id"  || $k=="translate_var_type" ){
	               if($v && is_numeric($v)){
	                   $v=(int)$v;
	                   $where[$k]=$v;
	               }
	           }else{
	               $v='%'.$v.'%';
	               $where[$k]=$v;
	           }
	           
	       }
	   }
	 //  echo '<pre>';print_r( $where);exit;
	 $rs=$this->translateContent->getList($where,$order,$limit);
	 $rs_count=$this->translateContent->getList($where);
	 $arr_translate_var_type=BAOGG_LANGUAGE::get('arr_translate_var_type');   
	 foreach((array)$rs as $k=>$v)
	 {
	 	$rs[$k]['translate_var_type']=$arr_translate_var_type[$v['translate_var_type']];
	 	$rs_var_content=$this->translateContent->getList(array("a.translate_var_id"=>(int)$v['translate_var_id']));
	 	
	 	foreach((array)$rs_var_content as $v)
	 	{
	 		$rs[$k]['content_'.$v['language_id']]=$v['content'];
	 	}
	 }	
     
	   echo  Zend_Json::encode(array("count"=>count($rs_count),"data"=>$rs));
	   
	   $this->_helper->viewRenderer->setNoRender ();
		
	}

	
	

	/**
	 * The delete action
	 */
	function deleteAction() {
        
	    $ret=array("success"=>false,"msg"=>"delete_fail");
	    $id = Baogg_Controller_Action::_getParam ( 'ids' );
		 

	    $objTranslateVar=new TranslateVar();
		if ($this->translateContent->_delete ( $id ) && $objTranslateVar->_delete($id)){
			$ret['success'] = true;
			$ret["msg"]="delete_ok";
		}
		
		echo Baogg_Language::outputResult($ret);
		$this->_helper->viewRenderer->setNoRender ();
		
		
		
	}

	/**
	 * The save action
	 */
	function saveAction() {
		//global $LANG;
		
	    $ret=array("success"=>false,"msg"=>"save_fail");
	    
	    //check form
		if (!$this->_request->isPost ())
		{
		   echo Baogg_Language::outputResult($ret);
		   exit;
		}
		
		$formData = $this->_request->getPost ();
        $formData['content']=$formData['content2']; //content only disp
		
		if(!$formData)
		{
		   echo Baogg_Language::outputResult($ret);
		   exit;     
		}
			
	    $arr=$this->translateContent->filterForm($formData);
         
	   
        
		$rs = $this->translateContent->getSame($arr);
		
		 
		//echo '<pre>';print_r($rs);exit;
		if($rs && $rs[0]){
			//update record
			//$permission_type = 2;
			//echo '<pre>';print_r($rs);var_dump(count($rs));exit;
			if( count($rs)==1 &&  $rs[0] == $arr["translate_var_id"] ){
			    $this->translateContent->_update($arr,$arr["translate_var_id"]);
			    $ret['success'] = true;				
				$ret['msg'] = "save_ok";			
			}else{
				$ret['msg'] = "exist_same_record";
			}
			
		}else{
			//insert record
			//$permission_type = 1;			
			if($rs){				
				$ret['msg'] = "exist_same_record";
			}else{
				
				$ret['success'] = $this->translateContent->_insert($arr)>0?true:false;
				$ret['msg'] = "add_ok";
			}
		}
				
		echo Baogg_Language::outputResult($ret);
		
		$this->_helper->viewRenderer->setNoRender ();
		
	}

}
